package com.quanyan.ic.vendingMachine.dao.impl;

import com.quanyan.ic.common.paging.Limit;
import com.quanyan.ic.vendingMachine.dao.VmPlaceMappingDAO;
import com.quanyan.ic.vendingMachine.mapper.VmPlaceMappingEntityMapper;
import com.quanyan.ic.vendingMachine.mapper.VmPosterEntityMapper;
import com.quanyan.ic.vendingMachine.mapper.VmUserPwdEntityMapper;
import com.quanyan.ic.vendingMachine.mapper.entity.*;
import com.quanyan.ic.vendingMachine.model.VmPlaceMapping;
import com.quanyan.ic.vendingMachine.model.VmPoster;
import com.quanyan.ic.vendingMachine.model.VmUserPwd;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhuo.shi on 2017/8/1.
 */
@Repository
public class VmPlaceMappingDAOImpl implements VmPlaceMappingDAO {

    @Autowired
    private VmPlaceMappingEntityMapper vmPlaceMappingEntityMapper;
    @Autowired
    private VmPosterEntityMapper vmPosterEntityMapper;
    @Autowired
    private VmUserPwdEntityMapper vmUserPwdEntityMapper;

    @Override
    public void insert(VmPlaceMapping mapping) {
        vmPlaceMappingEntityMapper.insertSelective(toEntity(mapping));
    }

    @Override
    public VmPlaceMapping findByVmNumber(String vmNumber) {
        if (StringUtils.isEmpty(vmNumber)) {
            return null;
        }
        VmPlaceMappingEntityExample example = new VmPlaceMappingEntityExample();
        example.createCriteria()
                .andVmNumberEqualTo(vmNumber);
        List<VmPlaceMappingEntity> resultList = vmPlaceMappingEntityMapper.selectByExample(example);
        if (resultList == null || resultList.size() <= 0) {
            return null;
        }
        return toModel(resultList.get(0));
    }

    @Override
    public long countPlaceVmSize(Integer placeId) {
        if (placeId == null) {
            return 0;
        }
        VmPlaceMappingEntityExample example = new VmPlaceMappingEntityExample();
        example.createCriteria()
                .andPlaceIdEqualTo(placeId);
        return vmPlaceMappingEntityMapper.countByExample(example);
    }

    @Override
    public List<VmPlaceMapping> queryPlaceVmMapping(Integer cityId, Integer placeId, Limit limit) {
        VmPlaceMappingEntityExample example = new VmPlaceMappingEntityExample();
        VmPlaceMappingEntityExample.Criteria criteria = example.createCriteria();
        if (cityId != null) {
            criteria.andCityIdEqualTo(cityId);
        }
        if (placeId != null) {
            criteria.andPlaceIdEqualTo(placeId);
        }
        criteria.andInUseEqualTo(true);
        example.setLimit(limit);
        List<VmPlaceMappingEntity> vmPlaceMappingEntities = vmPlaceMappingEntityMapper.selectByExample(example);
        if (vmPlaceMappingEntities == null || vmPlaceMappingEntities.size() <= 0) {
            return null;
        }
        List<VmPlaceMapping> vmPlaceMappings = new ArrayList<>();
        for (VmPlaceMappingEntity vmPlaceMappingEntity : vmPlaceMappingEntities) {
            vmPlaceMappings.add(toModel(vmPlaceMappingEntity));
        }
        return vmPlaceMappings;
    }

    @Override
    public long countPlaceVmMapping(Integer cityId, Integer placeId) {
        VmPlaceMappingEntityExample example = new VmPlaceMappingEntityExample();
        VmPlaceMappingEntityExample.Criteria criteria = example.createCriteria();
        if (cityId != null) {
            criteria.andCityIdEqualTo(cityId);
        }
        if (placeId != null) {
            criteria.andPlaceIdEqualTo(placeId);
        }
        criteria.andInUseEqualTo(true);
        return vmPlaceMappingEntityMapper.countByExample(example);
    }

    @Transactional
    @Override
    public void updatePlaceVmMappingExt(Integer placeId,
                                        List<VmPoster> poster, List<VmUserPwd> userPwds) {
        if (!CollectionUtils.isEmpty(poster)) {
            for (VmPoster vmPoster : poster) {
                VmPosterEntity entity = new VmPosterEntity();
                entity.setSmallImgUrl(vmPoster.getImgUrl_S());
                entity.setMiddleImgUrl(vmPoster.getImgUrl_M());
                entity.setLargeImgUrl(vmPoster.getImgUrl_L());
                entity.setPlaceId(placeId);
                vmPosterEntityMapper.insertSelective(entity);
            }
        }
        if (!CollectionUtils.isEmpty(userPwds)) {
            for (VmUserPwd userPwd : userPwds) {
                String userName = userPwd.getUserName();
                String password = userPwd.getPassword();
                if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
                    continue;
                }
                if (exsitUserPwd(placeId, userName, password)) {
                    continue;
                }
                VmUserPwdEntity entity = new VmUserPwdEntity();
                entity.setPlaceId(placeId);
                entity.setUserName(userName);
                entity.setPassword(password);
                vmUserPwdEntityMapper.insertSelective(entity);
            }
        }
    }

    @Override
    public void deletePlaceVmMapping(Integer id) {
        vmPlaceMappingEntityMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void disablePlaceVmMapping(Integer id) {
        VmPlaceMappingEntity entity = new VmPlaceMappingEntity();
        entity.setId(id);
        entity.setInUse(false);
        vmPlaceMappingEntityMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public List<VmPoster> findVmPoster(Integer placeId) {
        if (placeId == null) {
            return null;
        }
        VmPosterEntityExample example = new VmPosterEntityExample();
        example.createCriteria()
                .andPlaceIdEqualTo(placeId);
        List<VmPosterEntity> vmPosterEntities = vmPosterEntityMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(vmPosterEntities)) {
            return null;
        }
        List<VmPoster> posters = new ArrayList<>();
        for (VmPosterEntity entity : vmPosterEntities) {
            VmPoster vmPoster = new VmPoster();
            vmPoster.setPlaceId(placeId);
            vmPoster.setId(entity.getId());
            vmPoster.setImgUrl_S(entity.getSmallImgUrl());
            vmPoster.setImgUrl_M(entity.getMiddleImgUrl());
            vmPoster.setImgUrl_L(entity.getLargeImgUrl());
            posters.add(vmPoster);
        }
        return posters;
    }

    @Override
    public List<VmUserPwd> findVmUserPwd(Integer placeId) {
        if (placeId == null) {
            return null;
        }
        VmUserPwdEntityExample example = new VmUserPwdEntityExample();
        example.createCriteria()
                .andPlaceIdEqualTo(placeId);
        List<VmUserPwdEntity> entities = vmUserPwdEntityMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(entities)) {
            return null;
        }
        List<VmUserPwd> userPwds = new ArrayList<>();
        for (VmUserPwdEntity entity : entities) {
            VmUserPwd userPwd = new VmUserPwd();
            BeanUtils.copyProperties(entity, userPwd);
            userPwds.add(userPwd);
        }
        return userPwds;
    }

    private boolean exsitUserPwd(Integer placeId, String userName, String password) {
        if (placeId == null || StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            return false;
        }
        VmUserPwdEntityExample example = new VmUserPwdEntityExample();
        example.createCriteria()
                .andPlaceIdEqualTo(placeId)
                .andUserNameEqualTo(userName)
                .andPasswordEqualTo(password);
        return vmUserPwdEntityMapper.countByExample(example) > 0;
    }

    @Override
    public void deleteVmPoster(Integer id) {
        vmPosterEntityMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteVmUserPwd(Integer id) {
        vmUserPwdEntityMapper.deleteByPrimaryKey(id);
    }

    private VmPlaceMappingEntity toEntity(VmPlaceMapping model) {
        VmPlaceMappingEntity entity = new VmPlaceMappingEntity();
        BeanUtils.copyProperties(model, entity);
        return entity;
    }

    private VmPlaceMapping toModel(VmPlaceMappingEntity entity) {
        VmPlaceMapping model = new VmPlaceMapping();
        BeanUtils.copyProperties(entity, model);
        model.setInUse(entity.getInUse());
        return model;
    }
}
